#Include Lib\ReadMemory.ahk
#Include Lib\WriteMemory.ahk
#Include Lib\ImageFunctions.ahk

;4194304
;9460301 , E787A0


;13027E8
;130D538


getMapID(){
	base:=0xEB2AA8
	p1:=ReadMemory(base,"Atlantica")
	p1+=0x7FC
	p2:=ReadMemory(p1,"Atlantica")
	p2+=0xc
	p3:=ReadMemory(p2,"Atlantica")
	p3+=0x30
	p4:=ReadMemory(p3,"Atlantica")
	p4+=0xAA
	x:=ReadMemoryShortInt(p4,"Atlantica")
	SetFormat,IntegerFast,d
	x+=0
	Return x
}

setAutoMapId(mapNumber) {
	base:=0xEB2AA8
	p1:=ReadMemory(base,"Atlantica")
	p1+=0x15D8
	p2:=ReadMemory(p1,"Atlantica")
	p2+=0x78
	p3:=ReadMemory(p2,"Atlantica")
	p3+=0x894
	WriteMemoryShortInt(mapNumber,p3,"Atlantica")
	
}

setAutoMapX(posx) {
	base:=0xEB2AA8
	p1:=ReadMemory(base,"Atlantica")
	p1+=0x15D8
	p2:=ReadMemory(p1,"Atlantica")
	p2+=0x78
	p3:=ReadMemory(p2,"Atlantica")
	p3+=0x896
	WriteMemoryShortInt(posx,p3,"Atlantica")
	
}


setAutoMapY(posy) {
	base:=0xEB2AA8
	p1:=ReadMemory(base,"Atlantica")
	p1+=0x15D8
	p2:=ReadMemory(p1,"Atlantica")
	p2+=0x78
	p3:=ReadMemory(p2,"Atlantica")
	p3+=0x898
	WriteMemoryShortInt(posy,p3,"Atlantica")
	
}

toggleAutoMove(b){
	base:=0xEB2AA8
	p1:=ReadMemory(base,"Atlantica")
	p1+=0x15D8
	p2:=ReadMemory(p1,"Atlantica")
	p2+=0x78
	p3:=ReadMemory(p2,"Atlantica")
	p3+=0x88E
	
	WriteMemoryByte(b,p3,"Atlantica")
}


AutoMove(posx,posy,mapid) {
	setAutoMapID(mapid)
	setAutoMapX(posx)
	setAutoMapY(posy)
	toggleAutoMove(1)
}





getInCombat(){
x:=ReadMemory(0x0F2D84C,"Atlantica")
return x
}


GetX() {
	SetFormat,IntegerFast,hex
	base:=0xF2D844
	p1:=ReadMemory(base,"Atlantica")
	p1+=0x7e
	;p2:=ReadMemory(p1,"Atlantica")
	;p2+=0xC
	;p3:=ReadMemory(p2,"Atlantica")
	;p3+=0x30
	;p4:=ReadMemory(p3,"Atlantica")
	;p4+=0xB4
	x:=ReadMemoryShortInt(p1,"Atlantica")
	SetFormat,IntegerFast,d
	x+=0
	Return x
}



GetY() {
	SetFormat,IntegerFast,hex
	base:=0xF2D844
	p1:=ReadMemory(base,"Atlantica")
	p1+=0x80
	;p2:=ReadMemory(p1,"Atlantica")
	;p2+=0xC
	;p3:=ReadMemory(p2,"Atlantica")
	;p3+=0x30
	;p4:=ReadMemory(p3,"Atlantica")
	;p4+=0xB6
	x:=ReadMemoryShortInt(p1,"Atlantica")
	SetFormat,IntegerFast,d
	x+=0
	Return x
}


SetRunspeed(r) {
	SetFormat,IntegerFast,hex
	base:=0x13105E8
	p1:=ReadMemory(base,"Atlantica")
	p1+=0x800
	p2:=ReadMemory(p1,"Atlantica")
	p2+=0xC
	p3:=ReadMemory(p2,"Atlantica")
	p3+=0x28c
	WriteMemory(r,p3,"Atlantica")
	SetFormat,IntegerFast,d
}


SetSpeed(s){
	WriteMemory(s, 0xEE26A8,"Atlantica")
}
;Atlantica.exe+F957E0

ReadCursor() {
	static hand
	static sword
	static pickup
	if (!hand) {
		SetFormat,IntegerFast,hex
		cursorbase:= 0xEB2AA8
		cursor_p := ReadMemory(cursorbase,"Atlantica")
		cursor_p += 0xB24
		cursor:=ReadMemory(cursor_p,"Atlantica")
		hand_a := cursor+0x4
		sword_a := cursor+0x10
		pickup_a := cursor+0x14
		hand := ReadMemory(hand_a,"Atlantica")
		sword := ReadMemory(sword_a,"Atlantica")
		pickup := ReadMemory(pickup_a,"Atlantica")
		SetFormat,IntegerFast,d
		hand+=0
		sword+=0
		pickup+=0
	}

 	
	NumPut(VarSetCapacity(cursorInfo, A_PtrSize + 16), cursorInfo, "Uint"), DllCall("GetCursorInfo", "Uint", &cursorInfo, "Uint" )
	hi := NumGet(cursorInfo, 8)
	xi:=Num2Bin(hi,32)
	xi:=Bin2Num(xi)
	;msgbox, %sword_a%
	if (xi=hand) {

		return, 0
		}
	if (xi=sword) {

		return, 1
	}
	if (xi=pickup) {

		return, 2
	}  
	return, -1
		
}


SetBattleZoom(z) {
	SetFormat,IntegerFast,hex
	z:=FloatToHex(z)
	zoombase:= 0xF2D880
	zoom_p := ReadMemory(zoombase,"Atlantica")
	zoom_p += 0x38
	zoom_p2:=ReadMemory(zoom_p,"Atlantica")
	zoom_1:= zoom_p2+0x70
	zoom_2:= zoom_p2+0x100
	zoom_3:= zoom_p2+0x104
	zoom_4:= zoom_p2+0x128
	zoom_b:= zoom_p2+0xFC
	WriteMemory(z,zoom_1,"Atlantica")
	WriteMemory(z,zoom_2,"Atlantica")
	WriteMemory(z,zoom_3,"Atlantica")
	WriteMemory(z,zoom_4,"Atlantica")
	WriteMemory(z,zoom_b,"Atlantica")
	SetFormat,IntegerFast,d
}

SetCamera(d) {
	SetFormat,IntegerFast,hex
	d:=FloatToHex(d)
	camerabase:= 0xF2D880
	camera_p := ReadMemory(camerabase,"Atlantica")
	camera_p += 0x38
	camera_p2:=ReadMemory(camera_p,"Atlantica")
	camera:= camera_p2+0xDC
	WriteMemory(d,camera,"Atlantica")
	SetFormat,IntegerFast,d
}

FloatToHex(f) {
   form := A_FormatInteger
   SetFormat Integer, HEX
   v := DllCall("MulDiv", Float,f, Int,1, Int,1, UInt)
   SetFormat Integer, %form%
   Return v
}

decodeInteger(ptr) {
return *ptr | *++ptr << 8 | *++ptr << 16 | *++ptr << 24
}



Num2Bin(n,bits=0) {     ; Return LS "bits" of binary representation of "n"
   IfLess bits,1, Loop  ; n < 0: leading 1's are omitted. -1 -> 1, 0 -> 0
      {
         b := n&1 b
         n := n>>1
         If (n = n>>1)
            Break
      }
   Else Loop %bits%
      {
         b := n&1 b
         n := n>>1
      }
   Return b
}


Bin2Num(bits,neg="") {  ; Return number converted from the binary "bits" string
   n = 0                ; If "neg" is not 0 or empty, 11..1 assumed on the left
   Loop Parse, bits
      n += n + A_LoopField
   Return n - !(neg<1)*(1<<StrLen(bits))
}

GetTurn() {
	if (!inBattle()){
		return, -1
	}
    SetFormat,IntegerFast,hex
    ;base:=0x13105E8
	base:=0xEB2AA8

    p1:=ReadMemory(base,"Atlantica")
    p1+=0x8ec
    p2:=ReadMemory(p1,"Atlantica")
    p2+=0x8
    p3:=ReadMemory(p2,"Atlantica")
    p3+=0x7A50
    ;7A50
	turn:=ReadMemory(p3,"Atlantica")
	SetFormat,IntegerFast,d
	turn+=0
	return, turn
}

getHP(char) {
    SetFormat,IntegerFast,hex
	hp:=0
    base:=0xEB2AA8
    ap_p1:=ReadMemory(base,"Atlantica")
    ap_p1+=0x15D8
    ap_p2:=ReadMemory(ap_p1,"Atlantica")
    ap_p2+=0x78
    ap_p3:=ReadMemory(ap_p2,"Atlantica")
    ap_p3+=0x30
    ap_p4:=ReadMemory(ap_p3,"Atlantica")
    ap_p4+=0x1e60
	ap_p4+=(char-1)*0x788
	hp:=ReadMemory(ap_p4,"Atlantica")
	SetFormat,IntegerFast,d
	hp+=0
	;sleep, 100
	return, hp
}

getMP(char) {
    SetFormat,IntegerFast,hex
    base:=0xEB2AA8
    ap_p1:=ReadMemory(base,"Atlantica")
    ap_p1+=0x15d8
    ap_p2:=ReadMemory(ap_p1,"Atlantica")
    ap_p2+=0x78
    ap_p3:=ReadMemory(ap_p2,"Atlantica")
    ap_p3+=0x30
    ap_p4:=ReadMemory(ap_p3,"Atlantica")
    ap_p4+=0x1E64
	ap_p4+=(char-1)*0x788
	mp:=ReadMemory(ap_p4,"Atlantica")
	SetFormat,IntegerFast,d
	mp+=0
	return, mp
}

getActiveChar() {
	static charAddress
	if (!charAddress) {
		SetFormat,IntegerFast,hex
		;base:=0x13105E8
		base:=0xEB2AA8
		p1:=ReadMemory(base,"Atlantica")
		p1+=0x126e4
		p2:=ReadMemory(p1,"Atlantica")
		p2+=0x1f8
		p3:=ReadMemory(p2,"Atlantica")
		p3+=0x4a8
		}
	char:=ReadMemory(p3,"Atlantica")
	SetFormat,IntegerFast,d
	char+=1
	return char
}

getCombo(char) {
    SetFormat,IntegerFast,hex
    base:=0x13105E8
    ap_p1:=ReadMemory(base,"Atlantica")
    ap_p1+=0x800
    ap_p2:=ReadMemory(ap_p1,"Atlantica")
    ap_p2+=0xC
    ap_p3:=ReadMemory(ap_p2,"Atlantica")
    ap_p3+=0x30
    ap_p4:=ReadMemory(ap_p3,"Atlantica")
    ap_p4+=0xE4BF
	ap_p4+=(char-1)*0x30
	combo:=ReadMemoryByte(ap_p4,"Atlantica")
	SetFormat,IntegerFast,d
	combo+=0
	return, combo
}


getCharName() {
	winget, prid, PID, Atlantica
	SetFormat,IntegerFast,hex
	base:=0x13105E8
	p1:=ReadMemory(base,"Atlantica")
	p1+=0x800
	p2:=ReadMemory(p1,"Atlantica")
	p2+=0xC
	p3:=ReadMemory(p2,"Atlantica")
	p3+=0x30
	p4:=ReadMemory(p3,"Atlantica")
	p4+=0xCC
	x:=ReadMemory_Str(p4)
	SetFormat,IntegerFast,d
	Return x
	
}


 getProx(){
	static pa1
	static pa2
	static pa3
	static pa4
	static pa5
	static p1c:=-1
	static p2c:=-1
	static p3c:=-1
	static p4c:=-1
	static p5c:=-1
	
	winget, prid, PID, Atlantica
	SetFormat,IntegerFast,hex
	inc:=0x20
	base:=0x012787A0
	p1:=ReadMemory(base,"Atlantica")
	p1+=0x7f4
	p2:=ReadMemory(p1,"Atlantica")
	p2+=0x20
	
	loop, 30 {
		p3:=ReadMemory(p2,"Atlantica")
		p3+=0x7ec
		p4:=ReadMemory(p3,"Atlantica")
		p4+=0x2c

		;name:=ReadMemory_str(p4)
		;msgbox, nome %name% 

		Writememory(nada,p4,"Atlantica")
		p2+=0x4
	}
	
	p2:=ReadMemory(p1,"Atlantica")
	p2+=0x20
	p3:=readMemory(p2,"Atlantica")
	p3+=0x7ec
	p4:=Readmemory(p3,"Atlantica")
	p4+=0x2c
		
	sleep, 1000
	
	loop, 30 {
		name:=ReadMemory_str(p4)
		msgbox, n1 %name%
		nSize:=StrLen(String)
		tmp:=SubStr(name,nSize)
		;msgbox, %tmp%
		if (tmp="]") 
		{
			
			msgbox, n2 %name%
			if (name != pa1 && name!= pa2 && name!=pa3 && name!=pa4 && name!=pa5) 
			{
				
				if (pa1="" ) 
				{
					
					pa1:=name
					p1c:=3
					
				}
				else if (pa2="" ) 
				{
					pa2:=name
					p2c:=3
				}
				else if (pa3="" ) 
				{
					pa3:=name
					p3c:=3
				}
				else if (pa4="" ) 
				{
					pa4:=name
					p4c:=3
				}
				else if (pa5="" ) 
				{
					pa5:=name
					p5c:=3
				}
			
				
			else if (pa1=name) 
				{
				p1c+=2
				}
			else if (pa2=name) 
				{
				p2c+=2
				}
			else if (pa3=name) 
				{
				p3c+=2
				}
			else if (pa4=name) 
				{
				p4c+=2
				}
			else if (pa5=name) 
				{
				p5c+=2
				}
                        }

		
		
			
		}
			
			
			p3:=readMemory(p2,"Atlantica")
			p3+=0x7ec
			p4:=Readmemory(p3,"Atlantica")
			p4+=0x2c
			p1c--
			p2c--
			p3c--
			p4c--
			p5c--
			if (p1c<0){
				pa1=
			 }		
			if (p2c<0){
				pa2=
			}
			if (p3c<0){
				pa3=
			}
			if (p4c<0){
				pa4=
			}
			if (p5c<0){
				pa5=
			}
			
			
		}
			msgbox, %pa1%:%p1c% e %pa2%:%p2c% e %pa3%:%p3c% e %p4%:%p4c% e %p5%:%p5c%
		
		
		
	}


	

	




